package o;

import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.downloadengine.impl.DownloadService;
import com.huawei.appgallery.downloadengine.impl.SplitDownloadThreadInfo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class bnl {
    private static final Object ʻॱ = new Object();
    private static volatile bnl ˊ = null;
    private static final Object ˋ = new byte[0];
    private static bne ˏ = new bne();
    private boolean ʻ = true;
    private bmz ʼ;
    private bnd ʽ = new bnd();
    private String ˊॱ = "";
    private boolean ˋॱ = false;
    private final Object ˎ = new byte[0];
    private int ˏॱ = 1;
    private boolean ͺ = false;
    private List<SessionDownloadTask> ॱ = new ArrayList();
    private String ॱˊ = "";
    private Context ॱˋ = null;
    private ExecutorService ॱˎ = Executors.newSingleThreadExecutor();
    private boolean ॱॱ = false;
    private ExecutorService ॱᐝ = Executors.newSingleThreadExecutor();
    private Handler ᐝ;
    private ExecutorService ᐝॱ = Executors.newFixedThreadPool(6);

    private bnl() {
    }

    public static bnl ˏ() {
        bnl o_bnl;
        synchronized (ˋ) {
            if (ˊ == null) {
                ˊ = new bnl();
            }
            o_bnl = ˊ;
        }
        return o_bnl;
    }

    private static void ʻॱ() {
        synchronized (ʻॱ) {
            bnd ॱˊ = ˏ().ॱˊ();
            SharedPreferences sharedPreferences = ˏ().ॱ().getSharedPreferences("DownloadParam", 0);
            String string = sharedPreferences.getString("backupDomainInfo", null);
            if (TextUtils.isEmpty(string)) {
                Object string2 = sharedPreferences.getString("cdnBackupDomain", null);
                bnc.ˎ.ˋ("DownloadManager", "restore download params from cdnBackupDomain, cdnBackupDomain=" + string2);
                if (!TextUtils.isEmpty(string2)) {
                    ॱˊ.ˎ(Arrays.asList(string2.split(",")));
                }
            } else {
                bnc.ˎ.ˋ("DownloadManager", "restore download params from cdnBackupDomainInfo, cdnBackupDomainInfo=" + string);
                ॱˊ.ˎ(Arrays.asList(string.split(",")));
                ˏ().ˎ(string);
            }
            ॱˊ.ॱ(sharedPreferences.getString("httpDispatchBackupIp", null));
            ॱˊ.ˏ(sharedPreferences.getString("httpsDispatchBackupIp", null));
            ॱˊ.ˊ(sharedPreferences.getString("directIp", null));
            ॱˊ.ˋ(sharedPreferences.getString("directHost", null));
            ॱˊ.ˊ(sharedPreferences.getBoolean("isSupportDirectIpMode", false));
            ˏ().ˎ(sharedPreferences.getInt("supportDynamic", 1));
        }
    }

    public void ˎ() {
        bnm.ˎ(new SessionDownloadTask());
        bnm.ˎ(new SplitTask());
        bnm.ˎ(new SplitDownloadThreadInfo());
    }

    public void ˏ(Context context, fuv o_fuv) {
        this.ॱˋ = context;
        ᐝॱ();
        bnt.ˋ().ॱ();
        ʼॱ();
        ʻॱ();
        bno.ˋ(o_fuv);
    }

    public void ˊ(long j, int i) {
        SessionDownloadTask ˊ = ˊ(j);
        if (ˊ != null) {
            synchronized (ˊ) {
                bnc.ˎ.ˋ("HiAppDownload", "DownloadManager.pauseTask, reason=" + i + ॱॱ(ˊ));
                if (ˊ.ˈ() == 0) {
                    ˊ(ˊ, i);
                    ˊ.ॱॱ(6);
                    bny.ˏ(ˊ);
                    ˊ.ॱ(false);
                    bnf.ˊ(ˊ.ʾ());
                } else if (ˊ.ˈ() == 1 || ˊ.ˈ() == 2) {
                    ˊ(ˊ, i);
                    ˊ.ॱॱ(6);
                    bny.ˏ(ˊ);
                    ˊ.ॱ(false);
                } else if (ˊ.ˈ() == 6) {
                    ˊ.ॱ(true, i);
                    bnf.ˊ(ˊ.ʾ());
                }
                DownloadService.ˊ();
            }
            return;
        }
        bnc.ˎ.ˏ("HiAppDownload", "DownloadManager.pauseTask failed, no such task, id=" + j);
    }

    public void ˏ(long j) {
        SessionDownloadTask ˊ = ˊ(j);
        if (ˊ != null) {
            synchronized (ˊ) {
                if (ˊ.ʻ()) {
                    bnc.ˎ.ˋ("HiAppDownload", "DownloadManager.resumeTask task already scheduled " + ॱॱ(ˊ));
                    return;
                }
                bnc.ˎ.ˋ("HiAppDownload", "DownloadManager.resumeTask " + ॱॱ(ˊ));
                ˏ(ˊ);
                ᐝ(ˊ);
                bne.ˎ(ˊ);
                return;
            }
        }
        bnc.ˎ.ˏ("HiAppDownload", "DownloadManager.resumeTask failed, no such task, id=" + j);
    }

    public void ˋ(long j) {
        SessionDownloadTask ˊ = ˊ(j);
        if (ˊ != null) {
            synchronized (ˊ) {
                bnc.ˎ.ˋ("HiAppDownload", "DownloadManager.cancelTask" + ॱॱ(ˊ));
                ˊ(ˊ, 3);
                ˊ.ॱ(false);
                ˊ.ॱॱ(3);
                bny.ˋ(ˊ);
                bny.ˏ(ˊ);
                DownloadService.ˊ();
            }
            return;
        }
        bnc.ˎ.ˏ("HiAppDownload", "DownloadManager.cancelTask failed, no such task, id=" + j);
    }

    public void ˎ(SessionDownloadTask sessionDownloadTask) {
        Object obj = null;
        if (sessionDownloadTask != null) {
            synchronized (this.ˎ) {
                if (ˊ(sessionDownloadTask.ʾ()) != null) {
                    obj = 1;
                }
                if (obj == null) {
                    ˋ(sessionDownloadTask);
                }
                synchronized (sessionDownloadTask) {
                    ˏ(sessionDownloadTask);
                    sessionDownloadTask.ॱ(false);
                    sessionDownloadTask.ॱ(false, 0);
                    sessionDownloadTask.ॱॱ(6);
                    bny.ˏ(sessionDownloadTask);
                    sessionDownloadTask.ॱॱ(-1);
                    bny.ˏ(sessionDownloadTask);
                }
                if (obj == null) {
                    bne.ˋ(sessionDownloadTask);
                } else {
                    bnf.ˊ(sessionDownloadTask.ʾ());
                }
                bnc.ˎ.ˋ("HiAppDownload", "DownloadManager reserveTask , package=" + sessionDownloadTask.ˊˊ());
            }
        }
    }

    public List<SessionDownloadTask> ˋ() {
        List<SessionDownloadTask> arrayList = new ArrayList();
        synchronized (this.ˎ) {
            for (SessionDownloadTask sessionDownloadTask : this.ॱ) {
                if (sessionDownloadTask != null) {
                    arrayList.add(sessionDownloadTask);
                }
            }
        }
        return arrayList;
    }

    public SessionDownloadTask ˊ(long j) {
        SessionDownloadTask sessionDownloadTask;
        synchronized (this.ˎ) {
            for (SessionDownloadTask sessionDownloadTask2 : this.ॱ) {
                if (sessionDownloadTask2 != null && sessionDownloadTask2.ʾ() == j) {
                    break;
                }
            }
            sessionDownloadTask2 = null;
        }
        return sessionDownloadTask2;
    }

    public void ॱ(long j) {
        if (ˊ(j) != null) {
            bnc.ˎ.ˋ("HiAppDownload", "cancelTaskBySilent id=" + j);
            ˋ(j);
        }
    }

    public void ˊ(SessionDownloadTask sessionDownloadTask) {
        if (sessionDownloadTask != null && !TextUtils.isEmpty(sessionDownloadTask.ˊˊ())) {
            sessionDownloadTask.ˊ(System.currentTimeMillis());
            ˋ(sessionDownloadTask);
            ˏ(sessionDownloadTask);
            ᐝ(sessionDownloadTask);
            bne.ॱ(sessionDownloadTask);
            bnc.ˎ.ˋ("HiAppDownload", "DownloadManager.enqueueTask " + ॱॱ(sessionDownloadTask));
        }
    }

    private void ˊ(SessionDownloadTask sessionDownloadTask, int i) {
        if (sessionDownloadTask.ˑ() != null) {
            sessionDownloadTask.ˑ().ˏ(i);
        }
    }

    public Context ॱ() {
        return this.ॱˋ;
    }

    public boolean ˊ() {
        return !DownloadService.isAllDone();
    }

    public Handler ॱॱ() {
        return this.ᐝ;
    }

    public void ˏ(Handler handler) {
        this.ᐝ = handler;
    }

    bmz ʻ() {
        return this.ʼ;
    }

    public void ॱ(bmz o_bmz) {
        this.ʼ = o_bmz;
    }

    private void ᐝॱ() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("ACTION_ENQUEUE_TASK");
        intentFilter.addAction("ACTION_ENQUEUE_TASK_SAVE_FINISHED");
        intentFilter.addAction("ACTION_RESUME_TASK");
        intentFilter.addAction("ACTION_RESERVE_TASK");
        ll.ˏ(this.ॱˋ).ˏ(ˏ, intentFilter);
    }

    void ॱ(SessionDownloadTask sessionDownloadTask) {
        if (sessionDownloadTask != null) {
            synchronized (this.ˎ) {
                this.ॱ.remove(sessionDownloadTask);
            }
        }
    }

    void ˋ(SessionDownloadTask sessionDownloadTask) {
        synchronized (this.ˎ) {
            this.ॱ.add(sessionDownloadTask);
            bnc.ˎ.ˋ("HiAppDownload", "DownloadManager addTask, package=" + sessionDownloadTask.ˊˊ());
        }
    }

    boolean ᐝ() {
        return this.ॱॱ;
    }

    void ˋ(boolean z) {
        this.ॱॱ = z;
    }

    public void ˎ(boolean z) {
        this.ͺ = z;
    }

    public void ˎ(String str) {
        this.ॱˊ = str;
    }

    public boolean ʼ() {
        return this.ˋॱ;
    }

    public void ˊ(boolean z) {
        this.ˋॱ = z;
    }

    public boolean ʽ() {
        return this.ʻ;
    }

    public void ॱ(boolean z) {
        this.ʻ = z;
    }

    bnd ॱˊ() {
        return this.ʽ;
    }

    private void ॱˋ() {
        cdx.ˊ(new Runnable(this) {
            final /* synthetic */ bnl ॱ;

            {
                this.ॱ = r1;
            }

            public void run() {
                this.ॱ.ॱ(this.ॱ.ॱˋ);
            }
        });
    }

    private void ʾ() {
        synchronized (ʻॱ) {
            this.ॱˋ.getSharedPreferences("DownloadParam", 0).edit().putString("backupDomainInfo", this.ॱˊ).apply();
        }
        bnc.ˎ.ˋ("HiAppDownload", "saveCdnBackupDomainInfo success");
    }

    public String ͺ() {
        String string;
        synchronized (ʻॱ) {
            string = this.ॱˋ.getSharedPreferences("DownloadParam", 0).getString("backupDomainInfo", null);
        }
        return string;
    }

    public void ॱ(Map<String, String> map) {
        String str = (String) map.get("connectTimeout");
        if (!TextUtils.isEmpty(str)) {
            try {
                bno.ॱ(Integer.parseInt(str));
            } catch (NumberFormatException e) {
                bnc.ˎ.ˏ("HiAppDownload", "setPolicyParams NumberFormatException:connectTimeout");
            }
        }
        str = (String) map.get("readWriteTimeout");
        if (!TextUtils.isEmpty(str)) {
            try {
                bno.ˊ(Integer.parseInt(str));
            } catch (NumberFormatException e2) {
                bnc.ˎ.ˏ("HiAppDownload", "setPolicyParams NumberFormatException:readWriteTimeout");
            }
        }
        ˏ((Map) map);
        str = (String) map.get("supportDynamic");
        if (!TextUtils.isEmpty(str)) {
            try {
                int parseInt = Integer.parseInt(str);
                if (ˏ().ॱˊ() != null && parseInt != ˏ().ॱᐝ()) {
                    ˏ().ˎ(parseInt);
                    synchronized (ʻॱ) {
                        this.ॱˋ.getSharedPreferences("DownloadParam", 0).edit().putInt("supportDynamic", ˏ().ॱᐝ()).apply();
                    }
                }
            } catch (NumberFormatException e3) {
                bnc.ˎ.ˏ("HiAppDownload", "setPolicyParams NumberFormatException");
            }
        }
    }

    private void ˏ(Map<String, String> map) {
        this.ˊॱ = (String) map.get("backupDomainList");
        if (!TextUtils.isEmpty(this.ˊॱ)) {
            bnc.ˎ.ˋ("HiAppDownload", "backupDomainList from server is " + this.ˊॱ);
            this.ʽ.ˎ(Arrays.asList(this.ˊॱ.split(",")));
            ˋॱ();
        }
        String str = (String) map.get("directIP");
        if (!TextUtils.isEmpty(str)) {
            bnc.ˎ.ˋ("HiAppDownload", "directIP from server is " + str);
            this.ʽ.ˊ(str);
        }
        String str2 = (String) map.get("directHost");
        if (!TextUtils.isEmpty(str2)) {
            bnc.ˎ.ˋ("HiAppDownload", "directHost from server is " + str2);
            this.ʽ.ˋ(str2);
        }
        if (!(TextUtils.isEmpty(str) || TextUtils.isEmpty(str2))) {
            this.ʽ.ˊ(true);
        }
        str = (String) map.get("httpDispatchBackupIp");
        if (!TextUtils.isEmpty(str)) {
            bnc.ˎ.ˋ("HiAppDownload", "httpDispatchBackupIp from server is " + str);
            this.ʽ.ॱ(str);
        }
        str = (String) map.get("httpsDispatchBackupIp");
        if (!TextUtils.isEmpty(str)) {
            bnc.ˎ.ˋ("HiAppDownload", "httpsDispatchBackupIp from server is " + str);
            this.ʽ.ˏ(str);
        }
        ॱˋ();
    }

    public void ˋॱ() {
        cdx.ˊ(new Runnable(this) {
            final /* synthetic */ bnl ˎ;

            {
                this.ˎ = r1;
            }

            public void run() {
                this.ˎ.ˈ();
            }
        });
    }

    private void ˈ() {
        if (!this.ͺ) {
            this.ॱˊ = new bnp().ˊ(this.ʽ.ˋ());
            if (!TextUtils.isEmpty(this.ॱˊ)) {
                bnc.ˎ.ˋ("DownloadManager", "cdnBackupDomain speed measure result = " + this.ॱˊ);
                ʾ();
                this.ͺ = true;
            }
        }
    }

    public void ॱ(bna o_bna) {
        bni.ˊ(o_bna);
    }

    ExecutorService ˊॱ() {
        return this.ᐝॱ;
    }

    public ExecutorService ˏॱ() {
        return this.ॱᐝ;
    }

    public ExecutorService ॱˎ() {
        return this.ॱˎ;
    }

    private void ˏ(SessionDownloadTask sessionDownloadTask) {
        if (sessionDownloadTask.ͺॱ() == null) {
            sessionDownloadTask.ˋ(this.ʼ);
        }
    }

    private void ʼॱ() {
        bnc.ˎ.ˋ("HiAppDownload", "DownloadManager initDownload");
        for (SessionDownloadTask sessionDownloadTask : bnt.ˋ().ˊ()) {
            if (ˏ(this.ॱˋ, sessionDownloadTask.ˊˊ())) {
                bnt.ˋ().ॱ(sessionDownloadTask);
            } else {
                ˏ().ˋ(sessionDownloadTask);
                if (!(sessionDownloadTask.ˈ() == 6 || sessionDownloadTask.ˈ() == -1)) {
                    sessionDownloadTask.ॱॱ(6);
                    bny.ˏ(sessionDownloadTask);
                }
            }
        }
    }

    private boolean ˏ(Context context, String str) {
        if (context == null || str == null) {
            return false;
        }
        try {
            PackageManager packageManager = context.getPackageManager();
            if (packageManager == null) {
                bnc.ˎ.ˋ("HiAppDownload", "failed to get PackageManager");
                return false;
            } else if (3 == packageManager.getApplicationEnabledSetting(str)) {
                return true;
            } else {
                return false;
            }
        } catch (IllegalArgumentException e) {
            ʽॱ();
            return false;
        }
    }

    private void ʽॱ() {
    }

    private void ᐝ(SessionDownloadTask sessionDownloadTask) {
        synchronized (sessionDownloadTask) {
            sessionDownloadTask.ॱ(false);
            sessionDownloadTask.ॱ(false, 0);
            sessionDownloadTask.ॱॱ(0);
            bny.ˏ(sessionDownloadTask);
        }
    }

    private void ॱ(Context context) {
        if (context == null) {
            bnc.ˎ.ˊ("HiAppDownload", "DownloadManager saveParams, context is null");
            return;
        }
        synchronized (ʻॱ) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("DownloadParam", 0);
            sharedPreferences.edit().putString("httpDispatchBackupIp", ˏ().ॱˊ().ˎ()).putString("httpsDispatchBackupIp", ˏ().ॱˊ().ˊ()).putString("directIp", ˏ().ॱˊ().ˏ()).putString("directHost", ˏ().ॱˊ().ʻ()).putBoolean("isSupportDirectIpMode", ˏ().ॱˊ().ॱ()).apply();
            if (!TextUtils.isEmpty(this.ˊॱ)) {
                sharedPreferences.edit().putString("cdnBackupDomain", this.ˊॱ).apply();
            }
        }
    }

    private String ॱॱ(SessionDownloadTask sessionDownloadTask) {
        return new StringBuffer().append(" package=").append(sessionDownloadTask.ˊˊ()).append(", sessionId=").append(sessionDownloadTask.ʾ()).append(", dlType=").append(sessionDownloadTask.ˌ()).toString();
    }

    public int ॱᐝ() {
        return this.ˏॱ;
    }

    public void ˎ(int i) {
        if (i == 0) {
            this.ˏॱ = 0;
        } else {
            this.ˏॱ = 1;
        }
    }
}
